package com.shaunstone.web.sports.dal.dao.repo;

import javax.persistence.EntityManager;

import org.apache.activemq.transaction.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.shaunstone.web.sports.dal.model.Person;
import com.shaunstone.web.sports.dal.model.PersonTeamMapping;
import com.shaunstone.web.sports.dal.model.PersonTeamMappingPK;
import com.shaunstone.web.sports.dal.model.Team;

@Repository
@Transactional()
public class TeamRepositoryImpl implements TeamCustomRepository {

	@Autowired
	private EntityManager entityManager;

	@Override
	public void comst(Person person, String role, Integer teamId) {
		// TODO Auto-generated method stub
		if (person.getId() == 0) {
			entityManager.persist(person);
		}
		Team team = entityManager.find(Team.class, teamId);
		PersonTeamMappingPK personTeamMappingPK = new PersonTeamMappingPK(person.getId(), teamId);

		PersonTeamMapping personTeamMapping = new PersonTeamMapping();
		personTeamMapping.setId(personTeamMappingPK);
		personTeamMapping.setPerson(person);
		personTeamMapping.setTeam(team);
		personTeamMapping.setRole(role);

		team.addPersonTeamMapping(personTeamMapping);
		entityManager.persist(personTeamMapping);
		// entityManager.flush();
	}

}
